// pages/measure/index.js

var Scale = requirePlugin("ykb_measure_plugin")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    realTimeWeight: 0,
    bleState: "空闲",
    state: "paused",
    measureData: null,
    score: "0",
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  restart() {
    Scale.restart()
  },
  onLoad: function (options) {

    Scale.listen(data => {
      let { code, msg, bleState, state, realTimeWeight, devices } = data

      if (code && msg) {
        console.log(code, msg)
      }

      if (devices != undefined) {
        console.log("devices", devices)
        this.setData({
          list: devices
        })
      }

      if (code >= 1003 && code <= 1009) {
        if (realTimeWeight != undefined) {
          console.log("realTimeWeight", realTimeWeight)
          this.setData({
            realTimeWeight
          })
        }
        if (state) {
          this.setData({
            state
          })
        }
        this.setData({
          bleState
        })
      } else if (code == 1010) {
        console.log("data.result", data)
        this.setData({
          bleState,
          state,
          score: data.result.score,
          measureData: data.result
        })
        console.log("impedance", data.impedance)
        this.toast("测量完成")
      } else if (code == 1013) {
        this.setData({
          state,
          bleState
        })
      } else if (code == 10003) {
        this.setData({
          state,
          bleState
        })
      }
    })
  },
  toast(text) {
    wx.showToast({
      title: text,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    //Scale.listen(() => {})
  }
})